<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="../../common/bootstrap/css/bootstrap.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../css/customer_list.css" media="all">
</head>
<style>
    .layui-badge-rim + .layui-badge-rim {
        margin-left: 5px;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input id="userEdtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                    </div>
                    <div class="layui-inline">
                        <button id="userBtnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="userBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="userTable" lay-filter="userTable"></table>
        </div>
    </div>

</div>

<!-- 表格操作列 -->
<script type="text/html" id="userTableBar">
    <a class="layui-btn-a layui-btn-primary" lay-event="reset" style="color: #fff">重置密码</a>
    <a class="layui-btn-a" lay-event="edit" style="color: #fff">修改</a>
    {{# if(d.user_id !== 1){ }}
    <a class="layui-btn-a" lay-event="del" style="background-color: #FF794E; color: #fff">删除</a>
    {{# } }}
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="userForm">
    <form lay-filter="userForm" class="layui-form model-form">
        <input name="user_id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入账号" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" id="newPsw">
            <label class="layui-form-label">密码:</label>
            <div class="layui-input-block">
                <input type="password" name="newPsw" id="newPswIn" placeholder="请输入密码" class="layui-input"
                       lay-verify="required|psw"/>
            </div>
        </div>
        <div class="layui-form-item" id="rePsw">
            <label class="layui-form-label">确认密码:</label>
            <div class="layui-input-block">
                <input type="password" name="rePsw" id="rePswIn" placeholder="请再次输入密码" class="layui-input"
                       lay-verify="required|psw"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input name="true_name" placeholder="请输入用户名" type="text" class="layui-input"
                       maxlength="155" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="请输入手机号" type="text" class="layui-input"
                       lay-verify="required|phone" required/>
            </div>
        </div>
        <div class="layui-form-item" id="showRole">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="role_id" id="roleList" lay-verify="required" lay-search>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="userFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../../common/layui/layui2.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/main.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'formSelects', 'admin', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var page = 1;
        var roleList={};

        form.render();
        // 渲染表格
        table.render({
            elem: '#userTable',
            url: access_baseurl+'Manager/managerList.html',
            method:'POST',
            where: {},
            page: true,
            cols: [[
                {align: 'center',field: 'username',title: '账号'},
                {align: 'center',field: 'true_name',title: '真实姓名'},
                {align: 'center',field: 'phone',title: '手机号'},
                {align: 'center', field: 'role_name',title: '角色'},
                {align: 'center',field: 'create_time', title: '注册时间'},
                {align: 'left',toolbar: '#userTableBar',title: '操作'}
            ]],
            done: function (res, curr, count) {
                page = res.page;

            }

        });
        // 搜索按钮点击事件
        $('#userBtnSearch').click(function () {
            var value = $('#userEdtSearch').val();
            table.reload('userTable', {where: {words: value}});
        });

        // 添加按钮点击事件
        $('#userBtnAdd').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(userTable)', function (obj) {
            console.log(obj);
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此用户吗？', {
                    skin: 'layui-layer-admin'
                }, function (i) {
                    layer.close(i);
                    layer.load(2);
                    send_req('Manager/delete', {id:data.user_id}, function (res) {
                        layer.closeAll('loading');
                        layer.msg('删除成功', {icon: 1, time:300},function (){
                            var words = $('#userEdtSearch').val();
                            table.reload('userTable',{page :{curr:page}}, {where: {words: words}});
                        });
                    });
                });
            } else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定重置此用户的密码吗？<br><span style="color: red;">（初始密码为123456）</span>', {
                    skin: 'layui-layer-admin'
                }, function (i) {
                    layer.close(i);
                    layer.load(2);
                    send_req('Manager/resetPsw', {id:data.user_id}, function (res) {
                        layer.closeAll('loading');
                        layer.msg('修改成功', {icon: 1, time:300});
                    });
                });
            } else if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }
        });

        // 表单提交
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '500px',
                offset: '65px',
                title: data ? '修改用户' : '添加用户',
                content: $('#userForm').html(),
                success: function (layero, index) {
                    // console.log(layero)
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    setSelRole();
                    form.render();
                    if (data) {
                        $("#rePswIn").removeAttr('lay-verify');
                        $("#newPswIn").removeAttr('lay-verify');
                        $("#newPsw").hide();
                        $("#rePsw").hide();
                        form.val('userForm', data);
                    }
                    form.render();
                }
            });
        }
        form.on('submit(userFormSubmit)', function (d) {
            if(d.field.newPsw !== d.field.rePsw){
                layer.msg('两次密码输入不一致', {icon: 2});
                return false;
            }
            send_req('Manager/modifyUser', d.field, function (res) {
                layer.closeAll('page');
                layer.closeAll('loading');
                layer.msg('操作成功', {icon: 1, time:300},function (){
                    var words = $('#userEdtSearch').val();
                    table.reload('userTable',{page :{curr:page}}, {where: {words: words}});
                });
            });
            return false;
        });

        function setSelRole() {
            send_req('Common/basedata', {}, function (data) {
                roleList = data.role_list;
            },false);
            var roleStr='<option value="">全部角色</option>';
            for(var i=0;i<roleList.length;i++){
                roleStr+='<option value="'+roleList[i].role_id+'">'+roleList[i].role_name+'</option>';
            }
            $('#roleList').html(roleStr);
            form.render();
        }

        // 添加表单验证方法
        form.verify({
            psw: [/^[\S]{5,20}$/, '密码必须5到20位，且不能出现空格'],
        });
        //缓存当前操作的是哪个表格的哪个tr的哪个td
        $(document).off('mousedown','.layui-table-grid-down').on('mousedown','.layui-table-grid-down',function (event) {
            //直接记录td的jquery对象
            table._tableTrCurrr = $(this).closest('td');
        });

        //给弹出的详情里面的按钮添加监听级联的触发原始table的按钮的点击事件
        $(document).off('click','.layui-table-tips-main [lay-event]').on('click','.layui-table-tips-main [lay-event]',function (event) {
            var elem = $(this);
            var tableTrCurrr =  table._tableTrCurrr;
            if(!tableTrCurrr){
                return;
            }
            var layerIndex = elem.closest('.layui-table-tips').attr('times');
            layer.close(layerIndex);
            table._tableTrCurrr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
        });
    });
</script>
</body>
</html>